package scan.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import scan.po.AdressPO;
import scan.transaction.XAResourceManager;

import javax.sql.DataSource;
import javax.transaction.xa.XAException;
import java.sql.SQLException;

@Repository
public class SecondDao {
    @Autowired
    public DataSource secondDataSource;
    public void insert(AdressPO adressPO) throws SQLException, XAException {
        XAResourceManager xaResourceManager = new XAResourceManager(secondDataSource);
        // 事务启动前准备
        xaResourceManager.begin();
        // 执行SQL
        xaResourceManager.execute("insert into eshop_address(address_id, user_id,address_name,country_code,phone,province_code,city_code,region_code,detail,create_user_id,create_time,modify_user_id,modify_time)" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?)",adressPO);
        // 事务执行完毕处于准备阶段等待 TM 下达 commit 指令
        xaResourceManager.prepare();
    }
}
